HTMLify

index.html
Views: 59 | Author: cody
<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Submit Button Animation</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link rel='stylesheet' type='text/css' media='screen' href='style.css'>
</head>

<body>

    <button class="btn" type="button">
        <span class="btn__text">Submit</span>

        <svg class="btn__progress" viewBox="0 0 48 48" width="48px" height="48px">
            <circle class="btn__progress-track" r="20" cx="24" cy="24" fill="none" stroke="#c7cad1" stroke-width="8" />
            <circle class="btn__progress-fill" r="20" cx="24" cy="24" fill="none" stroke="#000000" stroke-width="8"
                transform="rotate(-90,24,24)" stroke-dasharray="125.66 125.66" stroke-dashoffset="125.66" />
            <polyline class="btn__progress-check" points="12,24 20,32 36,16" fill="none" stroke="#fff" stroke-width="4"
                stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="34 34" stroke-dashoffset="34" />
        </svg>
    </button>

    <script>
        window.addEventListener("DOMContentLoaded", () => {
            const btn = document.querySelector("button");
            var doneTimeout = null,
                resetTimeout = null;

            if (btn) {
                btn.addEventListener("click", function () {
                    const runClass = "btn--running";
                    const doneClass = "btn--done";
                    const submitDuration = 2000;
                    const resetDuration = 1500;

                    // fake the submission
                    this.disabled = true;
                    this.classList.add(runClass);

                    clearTimeout(doneTimeout);
                    clearTimeout(resetTimeout);

                    doneTimeout = setTimeout(() => {
                        this.classList.remove(runClass);
                        this.classList.add(doneClass);

                        // reset the button
                        resetTimeout = setTimeout(() => {
                            this.disabled = false;
                            this.classList.remove(doneClass);
                        }, resetDuration);
                    }, 600 + submitDuration);
                });
            }
        });
    </script>
</body>

</html>

Comments